package love.yuanmengrobot.als.business.dao.impl;/*
 *   @Studio:CHJY · 源梦科技
 *   @Author:zjj
 *   @Ide:IntelliJ IDEA
 *   @Creation time:2021/6/15 14:55
 */

import love.yuanmengrobot.als.business.dao.AcademicWarningDao;
import love.yuanmengrobot.als.utilPublic.JdbcUtil;
import love.yuanmengrobot.als.utilPublic.SqlString;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class AcademicWarningDaoImpl implements AcademicWarningDao {
    @Override
    public String getWarning(String stu_no) {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        String preTerm = getPreTerm(getTerm());

        int starYear=0;           //入学年份
        String sixthTerm="";      //第六学期
        String seventhTerm="";    //第七学期
        String nowTerm="";
        float get_limit_credit=0;
        float get_optional_credit=0;
        float get_requir_credit=0;

        int warning_1=0;              //上一学期必修课、专业限选课不及格课程数
        int warning_2=0;              //必修课、专业限选课累计不及格门数
        float warning_3=0;           //上一学期所获学分（第一至六学期）
        float warning_4=0;           //不及格课程累计学分
        float warning_5=0;           //截止第七学期，按教学计划累计未获得学分
        float optional_credit=-1;
        float limit_credit=-1;
        float reauir_credit=-1;
        try{
            starYear=2000+Integer.parseInt(stu_no.substring(0,2));
            sixthTerm=(starYear+2)+"-"+(starYear+3)+"_2";
            System.out.println("sixthTerm:"+sixthTerm);
            seventhTerm=(starYear+3)+"-"+(starYear+4)+"_1";
            System.out.println("seventhTerm:"+seventhTerm);
            nowTerm=getTerm();
            System.out.println("nowTerm:"+nowTerm);
            System.out.println("preTerm:"+preTerm);
            conn = JdbcUtil.getConnection();
            stmt = conn.createStatement();
            rs = stmt.executeQuery(SqlString.selectPlanFullJoinScore(stu_no));
            while (rs.next()){
                String plan_course_name=rs.getString(1);
                String course_name=rs.getString(2);
                String score=rs.getString(3);
                String course_type=rs.getString(4);
                if(course_type.contains("课")){
                    course_type=course_type.substring(0,course_type.length()-1);
                }
                String course_term=rs.getString(7);
                float credit=rs.getFloat(8);
                if(plan_course_name!=null&&optional_credit==-1){
                    optional_credit=rs.getFloat(5);
                    limit_credit=rs.getFloat(6);
                    reauir_credit=rs.getFloat(9);
                }

                if(course_name!=null&&
                        ("学科基础".equals(course_type)||
                                "通识必修".equals(course_type)||
                                "实践环节".equals(course_type)||
                                "专业限选".equals(course_type)||"专业基础必修".equals(course_type))&&!isPass(score)){
                    warning_2++;                      //必修课、专业限选课累计不及格门数
                    if(preTerm.equals(course_term)){
                        warning_1++;                //上一学期必修课、专业限选课不及格课程数
                    }
                }

                if(course_name!=null&&(sixthTerm.compareTo(preTerm)>0||sixthTerm.compareTo(preTerm)==0)&&course_term.equals(preTerm)&&isPass(score)){
                    warning_3=warning_3+credit;   //上一学期所获学分（第一至六学期）

                }

                if(course_name!=null&&!isPass(score)){
                    warning_4=warning_4+credit;    //不及格课程累计学分

                }

                if(nowTerm.compareTo(seventhTerm)>0){
                    if(course_name!=null&&plan_course_name!=null&&course_name.equals(plan_course_name)&&isPass(score)){
                        if("专业任选".equals(course_type)){
                            get_optional_credit=get_optional_credit+credit;
                        }else if("专业限选".equals(course_name)){
                            get_limit_credit=get_optional_credit+credit;
                        }else {
                            get_requir_credit=get_requir_credit+credit;
                        }
                    }
                }

            }

            if(nowTerm.compareTo(seventhTerm)>0){
                warning_5=(limit_credit-get_limit_credit)+(optional_credit-get_optional_credit)+(reauir_credit-get_requir_credit);
            }else {
                warning_5=0;
            }
            if(warning_1>=4||warning_2>=8||warning_3<8||warning_4>=35||warning_5>=35){
                return "红色";
            }
            if(warning_1==3||warning_2>=6||warning_3<12||warning_4>=25||warning_5>=25){
                return "橙色";
            }
            if(warning_1==2||warning_2>=4||warning_3<15||warning_4>=15||warning_5>=15){
                return "黄色";
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {//回收资源
            JdbcUtil.close(rs, stmt, conn);
        }
        return null;
    }

    public static String getTerm(){
        try{
            int studentYear = StudentCourseTermDaoImpl.getStudentYear();
            Date date = new Date();
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            Date start = df.parse( studentYear+"-09-01");
            Date end = df.parse( (studentYear+1)+"-02-15");
            if((date.after(start)||date.equals(start))&&date.before(end)){
                return studentYear+"-"+(studentYear+1)+"_1";
            }else {
                return studentYear+"-"+(studentYear+1)+"_2";
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return "";
    }

    public static String getPreTerm(String nowTerm){
        if(nowTerm.substring(nowTerm.length()-1, nowTerm.length()).equals("2")){
            StringBuilder sb = new StringBuilder(nowTerm);
            sb.replace(nowTerm.length() - 1, nowTerm.length(), "1");
            return sb.toString();
        }else {
            String substring = nowTerm.substring(0,4);
            int i = Integer.parseInt(substring);
            return (i-1)+"-"+i+"_2";
        }

    }

    public static boolean isPass(String score){
        if(StudentGraduationCourseDaoImpl.isInteger(score)){
            int scoreInt = Integer.parseInt(score);
            if(scoreInt>=60){
                return true;
            }else {
                return false;

            }
        }else {
            if("及格".equals(score)||"优".equals(score)||"良".equals(score)||"中".equals(score)||"合格".equals(score)){
                return true;
            }else {
                return false;
            }
        }
    }

}
